package com.csdj.questions.config;//package com.csdj.springcloud.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * mybaits plus 自动生成代码配置
 */
@Configuration("/sd")
public class MyBatisGeneratorConfig {

        public static void main(String[] args) {
            //项目的当前的路径
            String projectPath = System.getProperty("user.dir");
            Map<OutputFile,String> paths = new HashMap<>();
            paths.put(OutputFile.mapper,projectPath.concat("/questions/src/main/java/com/csdj/questions/mapper"));
            paths.put(OutputFile.entity,projectPath.concat("/questions/src/main/java/com/csdj/questions/entity"));
            paths.put(OutputFile.mapperXml,projectPath.concat("/questions/src/main/java/com/csdj/questions/mapper"));
            paths.put(OutputFile.serviceImpl,projectPath.concat("/questions/src/main/java/com/csdj/questions/service"));
            paths.put(OutputFile.service,projectPath.concat("/questions/src/main/java/com/csdj/questions/service"));
            paths.put(OutputFile.controller,projectPath.concat("/questions/src/main/java/com/csdj/questions/controller"));
            FastAutoGenerator.create("jdbc:mysql://120.46.134.11:3306/quaro", "admin", "123456")
                    .globalConfig(builder -> {
                        builder.author("刘宇") // 设置作者
                         .fileOverride() // 覆盖已生成文件
                                .disableOpenDir()
                                .outputDir(projectPath); // 指定输出目录
                    })
                    .packageConfig(builder -> {
                        builder.parent("com.csdj.questions") // 设置父包名
//                            .moduleName("") // 设置父包模块名
                                .serviceImpl("service")
                                .pathInfo(paths); // 设置mapperXml生成路径
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude("diachronic_record") // 设置需要生成的表名
                                .controllerBuilder().enableRestStyle()//配置restController
                                .mapperBuilder().enableBaseResultMap().enableBaseColumnList()
                                .serviceBuilder().formatServiceImplFileName("%sService");

                    })
                    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                    .execute();
        }

        // 最新版
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
}
